home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / DOCS / GCCDOC / DOC / AOUT.MS next >
Encoding:
Text File  |  1991-09-24  |  33.5 KB  |  1,314 lines

  1. .de Bs         \" Begin subsection
  2. .RT
  3. .sp \\n(PDu
  4. .ne 1.1
  5. .ll -\\n(QIu
  6. .in +\\n(QIu
  7. .ls 1
  8. .B \\$1
  9. \\&\\$2
  10. .ti +\\n(PIu
  11. ..
  12. .de Es        \" End subsection
  13. .ls
  14. .in -\\n(QIu
  15. .ll +\\n(QIu
  16. ..
  17. .de Bi        \" Begin Illustration
  18. .DS B
  19. .ls 1
  20. .cs R 25
  21. .cs I 25
  22. .cs B 25
  23. .ss 25
  24. .tr -\-
  25. .lc \-
  26. ..
  27. .de Ei        \"End Illustration
  28. .ls
  29. .DE
  30. .ss 0
  31. .cs R
  32. .cs I
  33. .cs B
  34. .tr --
  35. .lc .
  36. ..
  37. .if n .ds n1 *
  38. .if t .ds n1 \(dg
  39. .if n .ds n2 **
  40. .if t .ds n2 \(dd
  41. .lg 0
  42. .DA August 2, 1982
  43. .TL
  44. Description of a
  45. .UX
  46. \fI\kxa.out\h'|\nxu+2u'a.out\fP
  47. File
  48. .AU
  49. Matt Bishop\*(n2
  50. .AI
  51. Megatest Corporation
  52. 2900 Patrick Henry Drive
  53. Santa Clara, CA  95050
  54. .AB
  55. This document describes the format
  56. of executable files which run under
  57. the Fourth Berkeley Distribution of
  58. .UX ,
  59. Release 4.1.
  60. .AE
  61. .FS
  62. \*(n2Author's present address:
  63. Department of Computer Sciences,
  64. Purdue University,
  65. West Lafayette, IN  47907
  66. .FE
  67. .SH
  68. .ls 2
  69. Introduction
  70. .PP
  71. This document describes the format of
  72. .I a.out
  73. and object files.
  74. An
  75. .I a.out
  76. file is the executable output from the loader;
  77. an object file, the name of which usually ends
  78. in \fI.o\fP, is the relocatable input to the loader.
  79. The format described here is that of the
  80. Berkeley Software Distribution, Release 4.1
  81. (better known as ``4.1BSD'');
  82. be warned that
  83. there are substantial differences between this format
  84. and that of the Bell Laboratories Version 7
  85. (and Version 32/V) format.
  86. .PP
  87. These notes were written to help those working
  88. on symbolic debuggers like
  89. .I adb (1)
  90. or
  91. .I sdb (1),
  92. or disassemblers for object and
  93. .I a.out
  94. files like the
  95. .I unas (1)
  96. series of disassemblers
  97. or those who want to learn more about how
  98. .UX
  99. works.
  100. .PP
  101. .UX
  102. provides no convention for determining
  103. when numbers are to be written as hexadecimal,
  104. as decimal, and as octal;
  105. hence,
  106. those with a leading `0' are octal;
  107. those with a leading `0x' are hexadecimal;
  108. and all others are decimal.
  109. .SH
  110. Overview
  111. .PP
  112. .I A.out
  113. and object files both use the same format.
  114. There are five parts to these files:
  115. .DS B
  116. .ls 1
  117. .ta \w'0. 'u
  118. 1.    the header
  119. 2.    the program and data segments
  120. 3.    relocation information
  121. 4.    the symbol table
  122. 5.    the string table
  123. .ls
  124. .DE
  125. These are arranged in the above order.
  126. (Note that the text and data segment
  127. includes the uninitialized data, also
  128. called the
  129. .I bss .
  130. These follow the initialized data and are set to 0.)
  131. Each of these will be discussed in the following sections.
  132. After that, a sample object file will be analyzed in
  133. detail to illustrate how all that has been discussed actually works.
  134. .SH
  135. Header
  136. .PP
  137. The header contains information needed to load the program.
  138. Specifically, its format is:
  139. .DS B
  140. .ls 1
  141. .ta \w' relocation size'u+6m
  142. magic number    (4 bytes)
  143. text segment size    (4 bytes)
  144. data segment size    (4 bytes)
  145. bss segment size    (4 bytes)
  146. symbol table size    (4 bytes)
  147. entry point    (4 bytes)
  148. text relocation size    (4 bytes)
  149. data relocation size    (4 bytes)
  150. .ls
  151. .DE
  152. .PP
  153. The magic number is a number which indicates
  154. how the program is to be executed.
  155. There are three possible values:
  156. .Bs OMAGIC "(value 0407)"
  157. This is the oldest kind of executable program.
  158. The text segment is neither write-protected nor shared;
  159. hence, there is no padding between it and the data segment.
  160. The text immediately follows the header.
  161. .Es
  162. .Bs NMAGIC "(value 0410)"
  163. With this magic number, the text segment is write-protected and
  164. shared between all processes executing the file.
  165. As a result, the data segment begins at the first
  166. block following the text segment.
  167. .Es
  168. .Bs ZMAGIC "(value 0413)"
  169. This is very much like the NMAGIC magic number;
  170. the only difference is that the text segment
  171. begins at the block following the block containing the header;
  172. the space between the end of the header
  173. and the beginning of the text segment is padded with zeroes.
  174. The sizes of both the text and data segments are multiples of the block size.
  175. In this format, the pages of the file are not pre-loaded but
  176. are loaded into the image as needed.
  177. .Es
  178. .LP
  179. These differences are summarized in figure 1, below.
  180. .Bi
  181. .ta \w'+----------------+   'u +\w'+--------------+     'u
  182. .if t .ds In \h'\w' 'u'information\h'\w' 'u'
  183. .if n .ds In information\
  184. .sp
  185. +----------------+    +----------------+    +----------------+
  186. | OMAGIC  header |    | NMAGIC  header |    | ZMAGIC  header |
  187. +----------------+    +----------------+    +----------------+
  188. |  text segment  |    |  text segment  |    | padding to end |
  189. +----------------+    +----------------+    |    of block    |
  190. |  data segment  |    | padding to end |    +----------------+
  191. +----------------+    |    of block    |    |  text segment  |
  192. |   relocation   |    +----------------+    +----------------+
  193. |  \*(In  |    |  data segment  |    | padding to end |
  194. +----------------+    +----------------+    |    of block    |
  195. |  symbol table  |    |   relocation   |    +----------------+
  196. +----------------+    |  \*(In  |    |  data segment  |
  197. |  string table  |    +----------------+    +----------------+
  198. +----------------+    |  symbol table  |    | padding to end |
  199.     +----------------+    |    of block    |
  200.     |  string table  |    +----------------+
  201.     +----------------+    |   relocation   |
  202.         |  \*(In  |
  203.         +----------------+
  204.         |  symbol table  |
  205.         +----------------+
  206.         |  string table  |
  207.         +----------------+
  208. .sp
  209.   FIGURE 1.  A Comparison of the Types of Executable Files
  210. .sp
  211. .Ei
  212. .PP
  213. The
  214. .I "entry point"
  215. is the point at which execution begins.
  216. The value stored is the point
  217. .I "in core"
  218. (not in the
  219. .I a.out
  220. file) where the process will start.
  221. To find the equivalent point in the
  222. .I a.out
  223. file, count from the beginning of
  224. the text segment rather than from the beginning of the file.
  225. .PP
  226. The header is not loaded when the file is executed.
  227. .PP
  228. The file
  229. .I /usr/include/a.out.h
  230. provides some useful macros for dealing with
  231. .I a.out
  232. file formats.
  233. The header structure is defined there as
  234. .DS B
  235. .ls 1
  236. .ta \w'#define'u +\w'unsigned  'u +\w'a_dirsize  'u +4n
  237. struct exec {
  238.     long    a_magic;    /* magic number */
  239.     unsigned    a_text;    /* size of text segment */
  240.     unsigned    a_data;    /* size of initialized data */
  241.     unsigned    a_bss;    /* size of uninitialized data */
  242.     unsigned    a_syms;    /* size of symbol table */
  243.     unsigned    a_entry;    /* entry point */
  244.     unsigned    a_trsize;    /* size of text relocation */
  245.     unsigned    a_drsize;    /* size of data relocation */
  246. };
  247. .ls
  248. .DE
  249. Also, the macro
  250. .B N_TXTOFF
  251. will return the offset into the file at
  252. which the text segment begins.
  253. Macros to locate the symbol and string tables (\c
  254. .B N_SYMOFF
  255. and
  256. .B N_STROFF ,
  257. respectively) are also given (see figure 2).
  258. Finally, it also provides another macro (\c
  259. .B N_BADMAG )
  260. that checks for an illegal magic number.
  261. .Bi
  262. .if t .ds He \h'\w' 'u'header\h'\w' 'u'
  263. .if n .ds He header\
  264. .if t .ds Ar " \(->
  265. .if n .ds Ar }
  266. .ds Dn \v'0.2v'
  267. .ds Up \v'-0.2v'
  268. .ta \w'\fBN_TXTOFF\fP> 'u +\w'+-----------------------+ 'u
  269. .sp
  270.   macro            size of segment
  271.     +-----------------------+
  272.     |                       |    \*(Dn\(lt\*(Up 1024 bytes (\fBZMAGIC\fP)
  273.     |        \*(He        |    \(lk         or
  274.     |                       |    \*(Up\(lb\*(Dn 32 bytes (the others)
  275. \fBN_TXTOFF\fP\*(Ar    +-----------------------+
  276.     |                       |
  277.     |     text  segment     |    { \fIa_text\fP
  278.     |                       |
  279.     +-----------------------+
  280.     |                       |
  281.     |     data  segment     |    { \fIa_data\fP
  282.     |                       |
  283.     +-----------------------+
  284.     |                       |
  285.     |    text relocation    |    { \fIa_trsize\fP
  286.     |                       |
  287.     +-----------------------+
  288.     |                       |
  289.     |    data relocation    |    { \fIa_drsize\fP
  290.     |                       |
  291. \fBN_SYMOFF\fP\*(Ar    +-----------------------+
  292.     |                       |
  293.     |     symbol  table     |    { \fIa_syms\fP
  294.     |                       |
  295. \fBN_STROFF\fP\*(Ar    +-----------------------+
  296.     |                       |    \*(Dn\(lt\*(Up string table size is
  297.     |     string  table     |    \(lk  given by the first
  298.     |                       |    \*(Up\(lb\*(Dn 4 bytes in the table
  299.     +-----------------------+
  300. .sp
  301.             FIGURE 2.  Structure of an \fIa.out\fP File
  302. .sp
  303. .Ei
  304. .SH
  305. Text and Data Section
  306. .PP
  307. The
  308. .I text
  309. segment is the segment containing the machine code to be performed.
  310. In an object file, it contains a translation of the higher-level
  311. code in the corresponding object file;
  312. in an
  313. .I a.out
  314. file, it is the code in all the object files,
  315. preceded by the startup routine (which
  316. sets up the
  317. .I argc ,
  318. .I argv ,
  319. and
  320. .I environ
  321. pointers and then calls the procedure
  322. .I main() )
  323. and followed by all the library routines referenced in the program.
  324. .PP
  325. In object files, references may be made to variables, procedures, or
  326. labels not defined in that file (as, for example, using the C
  327. .I extern
  328. keyword).
  329. These are called
  330. .I "relocatable references" ;
  331. the text and data relocation segments record for which
  332. addresses this has happened and additional information to aid
  333. in the relocation (see that section for more details.)
  334. .PP
  335. The
  336. .I data
  337. segment provides space for both the
  338. initialized and uninitialized data;
  339. the uninitialized data is stored as 0.
  340. The stack grows downwards from
  341. the highest possible memory location
  342. (on the
  343. .SM
  344. VAX\c
  345. .LG
  346. , 0x7ffff400), and is automatically extended as needed.
  347. To extend the data segment, however, the
  348. .I brk (2)
  349. or
  350. .I sbrk (2)
  351. routine must be called.
  352. Again, if reference is made to an undefined symbol,
  353. the fact that that byte is (or those bytes are)
  354. relocatable is stored in the data relocation segment.
  355. .PP
  356. As indicated above, if the magic number is
  357. .B ZMAGIC ,
  358. the text segment begins on the first block after
  359. the block containing the header,
  360. and the sizes of the data and text segments
  361. are multiples of the block length.
  362. If the magic number is either
  363. .B ZMAGIC
  364. or
  365. .B NMAGIC ,
  366. the data segment begins at the first
  367. block boundary following the text segment.
  368. .SH
  369. Text and Data Relocation Segments
  370. .PP
  371. These segments tell the loader how to relocate
  372. bytes in the text and data segments.
  373. Each relocation datum uses the structure
  374. .DS B
  375. .ls 1
  376. .ta \w'#define'u +\w'unsigned  'u +\w'r_symbolnum:24,  'u +4n
  377. struct relocation_info {
  378.     int    r_address;    /* address to relocate */
  379.     unsigned    r_symbolnum:24,    /* symbol ordinal */
  380.         r_pcrel:1,    /* pc relative already */
  381.         r_length:2,    /* 0=byte,1=word,2=long */
  382.         r_extern:1,    /* sym val not included */
  383.         :4;    /* nothing, yet */
  384. };
  385. .ls
  386. .DE
  387. .PP
  388. Two pieces of information are used to relocate something.
  389. The first is the contents of the location being relocated, and
  390. the second is the relocation datum in the relocation segment.
  391. When used, the first is the offset of that location from the
  392. beginning of the segment with respect to which it is relocated.
  393. In the relocation datum, the field
  394. .I r_address
  395. gives the address to be relocated;
  396. it is relative to the appropriate segment.
  397. The field
  398. .I r_symbolnum
  399. gives the number of the symbol table entry
  400. of the symbol which is being relocated.
  401. If it is relocated relative to the program counter,
  402. the bit
  403. .I r_pcrel
  404. is set;
  405. if the symbol's value is to be added to
  406. the contents of the location being relocated,
  407. the bit
  408. .I r_extern
  409. Finally, the
  410. .I r_length
  411. field gives the type of the datum
  412. (a byte, a word, or a longword) to be relocated.
  413. The second longword of the relocation datum is filled by
  414. the last four bit field.
  415. This structure is illustrated in figure 3.
  416. .Bi
  417. .ta \w'+------------+   'u
  418. .if t .ds Rl \|relocated\|
  419. .if n .ds Rl relocated\
  420. .sp
  421.  symbol table                relocation datum
  422. +------------+    +---------------------------------------+
  423. |   symbol   |    |               r_address               |
  424. |   to  be   |    +-----------+-------+--------+--------+-+
  425. | \*(Rl<-r_symbolnum|r_pcrel|r_length|r_extern| |
  426. +------------+    +-----------+-------+--------+--------+-+
  427. .sp
  428.          FIGURE 3. Picture of a Relocation Datum
  429. .sp
  430. .Ei
  431. .PP
  432. See the example, below, for a very detailed example of
  433. how the loader uses this information to relocate an address.
  434. .SH
  435. Symbol and String Tables
  436. .PP
  437. The contents of the symbol table varies,
  438. depending on the flags given to the compilers,
  439. such as
  440. .I cc (1),
  441. .I f77 (1),
  442. or
  443. .I pc (1),
  444. and the loader
  445. .I ld (1).
  446. The compiler flag that produces the most information is
  447. .I g ;
  448. without it, the usefulness of the debuggers
  449. .I adb (1)
  450. and
  451. .I sdb (1)
  452. is minimal.
  453. This section describes the symbol table,
  454. and in particular the format of the information
  455. placed there by the
  456. .I g
  457. flag.
  458. .PP
  459. The string table begins with an integer
  460. (four bytes) which gives its length.
  461. The names of the variables, each null-terminated, follow;
  462. every symbol table entry contains an index into this table.
  463. The string table exists because the 4.1BSD C compiler allows
  464. variables of arbitrary length;
  465. thus, it is not possible to reserve space in advance within the
  466. symbol table entry itself for the variable name
  467. (as is done in Version 7 symbol table format).
  468. A picture of all this is given in figure 4.)
  469. .Bi
  470. .ta +\w'+---------------------+        'u +\w'        'u
  471. .sp
  472.   symbol table entry             string table
  473. +---------------------+        +------------+
  474. | n_un  (name pointer)+    | _str1\e0    |
  475. +------+-------+------+    |    +------------+
  476. |n_type|n_other|n_desc|    +> _str2\e0    |
  477. +------+-------+------+        +------------+
  478. |       n_value       |        | _str3\e0    |
  479. +---------------------+        +------------+
  480. .sp
  481.   FIGURE 4.  Symbol Table Entry and Associated Name
  482. .sp
  483. .Ei
  484. .PP
  485. The structure of a symbol table entry is
  486. .DS B
  487. .ls 1
  488. .ta \w'#define  'u +\w'char'u-1u +\w'unsigned  'u+1u +\w'*n_name  'u
  489. struct nlist {
  490.     union {
  491.         char    *n_name;    /* for use when in-core */
  492.         long    n_strx;    /* index in file table */
  493.     } n_un;
  494.     unsigned char    n_type;    /* type flag (eg N_TEXT) */
  495.     char    n_other;    /* unused */
  496.     short    n_desc;    /* more detailed info */
  497.     unsigned    n_value;    /* value (or sdb offset) */
  498. };
  499. .ls
  500. .DE
  501. Note the union;
  502. it can contain either the offset of the variable name
  503. from the start of the string table
  504. (this includes the leading four bytes of the table;
  505. hence, the first name has offset 4),
  506. or a pointer to the variable name.
  507. The first is most useful when the string table is not read
  508. into memory;
  509. however, in most applications it is easier to read it in,
  510. and then the union value is merely changed to be the
  511. in-core address of the variable name.
  512. .PP
  513. There are four other parts to a symbol table entry;
  514. the contents of each part depends on what the type of the symbol is.
  515. This is recorded in the
  516. .I n_type
  517. field.
  518. .PP
  519. The
  520. .I n_type
  521. field,
  522. as its name suggests, contains the type of the symbol.
  523. It also contains the segment into which the symbol is placed.
  524. Possible values are:
  525. .DS B
  526. .ls 1
  527. .ta \w'N_COMM  'u +\w'(0x00)  'u
  528. N_UNDF    (0x00)    not associated with any segment
  529. N_EXT    (0x01)    defined externally
  530. N_ABS    (0x02)    absolute address (value) given
  531. N_TEXT    (0x04)    located in the text segment
  532. N_DATA    (0x06)    located in the initialized data segment
  533. N_BSS    (0x08)    located in the uninitialized data segment
  534. N_COMM    (0x12)    names a common region (internal to loader)
  535. N_FN    (0x1f)    names a file
  536. .ls
  537. .DE
  538. Note that there are three ``miscellaneous'' possibilities:
  539. if the symbol is a file name,
  540. .I n_type
  541. is set to
  542. .B N_FN ;
  543. if it names a common region,
  544. .I n_type
  545. is set to
  546. .B N_COMM
  547. (this value set, and used, only by the loader);
  548. and if symbol is external, the mask
  549. .B N_EXT
  550. is or'ed in.
  551. .PP
  552. As indicated above, by using the
  553. .I g
  554. flag when compiling and loading, it is
  555. possible to get more detailed type declarations.
  556. What the other fields of the symbol table entry mean
  557. depends on the value of
  558. .I n_type .
  559. Values defined in 4.1BSD are:
  560. .Bs N_GSYM "(value 0x20)"
  561. The symbol is a global symbol.
  562. The
  563. .I n_other
  564. and
  565. .I n_value
  566. fields are 0;
  567. the
  568. .I n_un
  569. field names the symbol;
  570. the
  571. .I n_desc
  572. field gives a more detailed type description.
  573. .Es
  574. .Bs N_FNAME "(value 0x22)"
  575. The symbol is a
  576. .SM
  577. FORTRAN-77
  578. .LG
  579. procedure name.
  580. The
  581. .I n_other
  582. field is 0;
  583. the
  584. .I n_un
  585. field names the procedure;
  586. the other fields are meaningless.
  587. .Es
  588. .Bs N_FUN "(value 0x24)"
  589. The symbol is the name of a procedure.
  590. The
  591. .I n_un
  592. field names the procedure;
  593. the
  594. .I n_other
  595. field is 0;
  596. the
  597. .I n_desc
  598. field contains the source file line number where it begins,
  599. and the
  600. .I n_value
  601. field contains the address in the object file
  602. where the procedure begins.
  603. .Es
  604. .Bs N_STSYM "(value 0x26)"
  605. The symbol is the name of a static symbol.
  606. The
  607. .I n_un
  608. field names the symbol;
  609. the
  610. .I n_other
  611. field is 0;
  612. the
  613. .I n_value
  614. field gives the variable's address,
  615. and the
  616. .I n_desc
  617. field contains a more detailed type description.
  618. .Es
  619. .Bs N_LCSYM "(value 0x28)"
  620. The symbol is a symbol local to the file and
  621. allocated in the uninitialized data segment.
  622. The symbol table parts have the same meaning as for
  623. .B N_STSYM .
  624. .Es
  625. .Bs N_PC "(value 0x30)"
  626. This is a type produced by the Berkeley
  627. .I pc (1)
  628. compiler.
  629. It is used to check types across separately compiled files.
  630. The
  631. .I n_un
  632. field contains the name of the symbol, the
  633. .I n_other
  634. field is 0, the
  635. .I n_value
  636. field contains the line number on
  637. which it occurs, and the
  638. .I n_desc
  639. field contains type information
  640. Values of the
  641. .I n_desc
  642. field are:
  643. .DS B
  644. .ls 1
  645. .ta \w'0. 'u +\w'included file name 'u +\w'06. 'u
  646. .Rt
  647. 1.    source file name    \06.    global variable
  648. 2.    included file name    \07.    global function
  649. 3.    global label    \08.    global procedure
  650. 4.    global constant    \09.    external function
  651. 5.    global type    \o'\01'0.    external procedure
  652. .ls
  653. .DE
  654. .Es
  655. .Bs N_RSYM "(value 0x40)"
  656. The symbol is one allocated to a register;
  657. the symbol table parts have the same meaning as for
  658. .B N_STSYM ,
  659. except that
  660. .I n_value
  661. is the register number, not the address.
  662. .Es
  663. .Bs N_SLINE "(value 0x44)"
  664. This marks the location where a new source line begins.
  665. Both the
  666. .I n_un
  667. and
  668. .I n_other
  669. fields are 0,
  670. .I n_desc
  671. contains the source line number, and
  672. .I n_value
  673. contains the address corresponding to the beginning of the line.
  674. .Es
  675. .Bs N_SSYM "(value 0x60)"
  676. This holds information about an element of a structure.
  677. The symbol table entry is interpreted just as that of
  678. .B N_STSYM ,
  679. except that
  680. .I n_value
  681. is the offset to be added to the variable's address to obtain
  682. the address of this structure element for that variable.
  683. .Es
  684. .Bs N_SO "(value 0x64)"
  685. The name of a source file (\c
  686. .I not
  687. an include file) has its
  688. .I n_type
  689. field set to this value.
  690. The
  691. .I n_un
  692. field is the name of the source file,
  693. .I n_other
  694. and
  695. .I n_desc
  696. are 0, and
  697. .I n_value
  698. is the address corresponding to the beginning of the source file.
  699. .Es
  700. .Bs N_LSYM "(value 0x80)"
  701. Local symbols have this as the value of the
  702. .I n_type
  703. field.
  704. The
  705. .I n_un ,
  706. .I n_other ,
  707. and
  708. .I n_desc
  709. fields are the same as for
  710. .B N_STSYM ,
  711. and the
  712. .I n_value
  713. field is an offset into the stack
  714. (recall that C puts local variables on the system stack).
  715. .Es
  716. .Bs N_SOL "(value 0x84)"
  717. This indicates that the symbol represents a file which
  718. is included by an ``#include'' statement.
  719. Its symbol table interpretation is identical to that of
  720. .B N_SO .
  721. .Es
  722. .Bs N_PSYM "(value 0xa0)"
  723. A symbol with this entry is a subroutine, procedure, or function parameter.
  724. Since parameters are pushed onto the system stack,
  725. these entries are interpreted just like those of
  726. .B N_LSYM .
  727. .Es
  728. .Bs N_ENTRY "(value 0xa4)"
  729. This represents an alternate entry point;
  730. its interpretation is just like that of
  731. .B N_ENTRY .
  732. .Es
  733. .Bs N_LBRAC "(value 0xc0)"
  734. This marks the occurrence of a left curly brace (`{').
  735. The
  736. .I n_un
  737. and
  738. .I n_other
  739. fields are 0, the
  740. .I n_value
  741. field contains the address corresponding to the left bracket,
  742. and the
  743. .I n_desc
  744. field contains the nesting level.
  745. .Es
  746. .Bs N_RBRAC "(value 0xe0)"
  747. This marks the occurrence of a right curly brace (`}').
  748. All fields are interpreted as for
  749. .B N_LBRAC .
  750. .Es
  751. .Bs N_BCOMM "(value 0xe2)"
  752. This signals the beginning of common.
  753. Only the name of the common is given
  754. (in the
  755. .I n_un
  756. field);
  757. the rest of the entry is meaningless.
  758. .Es
  759. .Bs N_ECOMM "(value 0xe4)"
  760. This signals the end of common.
  761. As with
  762. .B N_BCOMM ,
  763. only the name of the common is given
  764. (in the
  765. .I n_un
  766. field);
  767. the rest of the entry is meaningless.
  768. .Es
  769. .Bs N_ECOML "(value 0xe8)"
  770. This signals the end of a common block local to the routine using it;
  771. all fields except
  772. .I n_value ,
  773. which contains the address of that reference, are meaningless.
  774. .Es
  775. .Bs N_LENG "(value 0xfe)"
  776. This entry contains the number of bytes to allocate to a variable.
  777. The variable name is in the
  778. .I n_name
  779. field, the
  780. .I n_desc
  781. field is 0, and the variable's length in bytes in the
  782. .I n_value
  783. field.
  784. The
  785. .I n_other
  786. field is 1 to indicate this is a length entry.
  787. .Es
  788. .PP
  789. Most of the above formats use
  790. .I n_desc
  791. to describe the type further.
  792. In these cases, that field is divided into seven parts,
  793. as described by the structure
  794. .I desc :
  795. .DS B
  796. .ls 1
  797. .ta \w'#define\ 'u +\w'unsigned\ \ 'u +\w'basic:4   'u
  798. struct desc {
  799.     unsigned    q6:2,    /* least significant ... */
  800.     unsigned    q5:2,
  801.     unsigned    q4:2,
  802.     unsigned    q3:2,
  803.     unsigned    q2:2,
  804.     unsigned    q1:2,    /* to most significant */
  805.     unsigned    basic:4;    /* basic type */
  806. };
  807. .ls
  808. .DE
  809. The value of the basic field is the basic type of the symbol;
  810. the sixteen possible values are:
  811. .DS B
  812. .ls 1
  813. .ta \w'0. 'u +\w'function argument    'u +\w'00. 'u
  814. 0.    undefined    \08.    struct
  815. 1.    function argument    \09.    union
  816. 2.    char    10.    enum
  817. 3.    short int    11.    member of enum
  818. 4.    int    12.    unsigned char
  819. 5.    long int    13.    unsigned short int
  820. 6.    float    14.    unsigned int
  821. 7.    double    15.    unsigned long int
  822. .ls
  823. .DE
  824. The
  825. .I n_desc
  826. field allows up to six modifiers, in the
  827. .I q
  828. fields (\c
  829. .I q1
  830. is the most significant and
  831. .I q6
  832. the least significant),
  833. chosen from among:
  834. .DS B
  835. .ls 1
  836. .ta \w'0. 'u +\w'pointer to   'u +\w'0. 'u
  837. 0.    none    2.    function returning
  838. 1.    pointer to    3.    array of
  839. .ls
  840. .DE
  841. Thus, for example, the variable
  842. .I xxx
  843. which is declared as
  844. .DS C
  845. char (**(*xxx)())[]
  846. .DE
  847. would have the value 0x3592 in its
  848. .I n_desc
  849. that is,
  850. .I q6
  851. is 0,
  852. .I q5
  853. is 3 (array of),
  854. .I q4
  855. is 1 (pointer to),
  856. .I q3
  857. is 1 (pointer to),
  858. .I q2
  859. is 2 (function returning),
  860. .I q1
  861. is 1 (pointer to), and
  862. .I basic
  863. is 2 (char);
  864. in English, then,
  865. .I xxx
  866. is ``a pointer to a function returning a pointer to a pointer
  867. to an array of chars.''
  868. .SH
  869. Example
  870. .PP
  871. In this section, we will examine an object file;
  872. this will show how the information discussed in the
  873. preceding sections is actually used.
  874. The program being discussed is a very simple one;
  875. given a file name as an argument,
  876. it determines if the directories on the path are
  877. searchable and if so, whether the
  878. named file exists:
  879. .DS B
  880. .ls 1
  881. .ta \w'char **a'u +\w'if (argc'u
  882. /*
  883. .ti +\w'/'u
  884. * program to see if a file can be found
  885. .ti +\w'/'u
  886. *
  887. .ti +\w'/'u
  888. * only arg is file's path name
  889. .ti +\w'/'u
  890. */
  891. extern int errno;
  892.  
  893. main(argc, argv)
  894. int argc;
  895. char **argv;
  896. {
  897.     register int i;
  898.     char *oops = "error is";
  899.  
  900.     if (argc != 2){
  901.         printf("%s: too few arguments\n", argv[0]);
  902.         exit(0);
  903.     }
  904.  
  905.     if (access(argv[1], 0) == -1)
  906.         perror(oops);
  907.  
  908.     exit(errno);
  909. }
  910. .ls
  911. .DE
  912. First, let us look at the symbol and string tables
  913. of the object file.
  914. Using
  915. .I osho (1),
  916. a program which dumps the parts of an object (or executable)
  917. file without any interpretation,
  918. we see that the symbol table is:
  919. .DS B
  920. .ls 1
  921. .ta \w'00: 'u +\w'00, 0x000, 0, 00, 0x000   'u +\w'00: 'u
  922. \00:    4, 0x64, 0, 0, 0x0    17:    0, 0xc0, 0, 2, 0xc
  923. \01:    8, 0x20, 0, 4, 0x0    18:    0, 0x44, 0, 16, 0x12
  924. \02:    14, 0xfe, 1, 0, 0x4    19:    59, 0x1, 0, 0, 0x0
  925. \03:    20, 0x24, 0, 9, 0x0    20:    0, 0x40, 0, 17, 0x25
  926. \04:    25, 0x5, 0, 0, 0x0    21:    67, 0x1, 0, 0, 0x0
  927. \05:    31, 0xa0, 0, 4, 0x4    22:    0, 0x44, 0, 18, 0x2e
  928. \06:    36, 0xfe, 1, 0, 0x4    23:    0, 0x44, 0, 19, 0x2e
  929. \07:    41, 0xa0, 0, 82, 0x8    24:    0, 0x44, 0, 20, 0x2e
  930. \08:    46, 0x2, 0, 0, 0x800    25:    73, 0x1, 0, 0, 0x0
  931. \09:    0, 0x44, 0, 11, 0x2    26:    0, 0x44, 0, 21, 0x47
  932. 10:    0, 0x44, 0, 12, 0x4    27:    81, 0x1, 0, 0, 0x0
  933. 11:    50, 0x40, 0, 4, 0xb    28:    0, 0x44, 0, 22, 0x51
  934. 12:    52, 0xfe, 1, 0, 0x4    29:    0, 0x44, 0, 23, 0x51
  935. 13:    0, 0x44, 0, 13, 0x4    30:    89, 0x1, 0, 0, 0x0
  936. 14:    54, 0x80, 0, 18, 0x4    31:    0, 0x44, 0, 24, 0x5e
  937. 15:    0, 0x44, 0, 14, 0xc    32:    0, 0xe0, 0, 2, 0x5e
  938. 16:    0, 0x44, 0, 15, 0xc
  939. .ls
  940. .DE
  941. .PP
  942. The numbers before the colons indicate the ordering of
  943. symbol table entries.
  944. In each quintuplet, the first number is the offset into the
  945. string table of the name of the variable (see figure 4, above);
  946. the second (which
  947. .I osho (1)
  948. prints in decimal, but which has been translated into hexadecimal here)
  949. is the type;
  950. the third,
  951. .I n_other ;
  952. the fourth,
  953. .I n_desc ;
  954. and the last,
  955. .I n_value .
  956. .PP
  957. In order to interpret this table properly,
  958. we need to know what the string table looks like;
  959. a listing of it, in which each entry is preceded
  960. by its offset in the table, is:
  961. .DS B
  962. .ls 1
  963. .ta \w'00: 'u +\w'errno   'u +\w'00: 'u +\w'argc   'u +\w'00: 'u +\w'_printf   'u +\w'00: 'u
  964. \04:    x.c    31:    argc    50:    i    67:    _exit
  965. \08:    errno    36:    argc    52:    i    73:    _access
  966. 14:    errno    41:    argv    54:    oops    81:    _perror
  967. 20:    main    46:    L13    59:    _printf    89:    _errno
  968. 25:    _main
  969. .ls
  970. .DE
  971. (Recall the first four bytes give the length of the
  972. table.)
  973. .PP
  974. We can now interpret the symbol table.  Entry 0, for example, is
  975. for ``main.c'' (the name in the symbol table with offset 4),
  976. which, as the field
  977. .I n_type
  978. indicates, is a source file name, and the machine code for it
  979. begins at location 0
  980. (recall that since the header is not loaded,
  981. this means the beginning of the text segment.)
  982. To take a more complicated example, look at entry 11,
  983. which refers to the symbol ``i''.
  984. It is a register variable (\c
  985. .I n_type
  986. is N_RSYM, or 0x40)
  987. to which r11 is allocated
  988. (from the
  989. .I n_desc
  990. field, which is hexadecimal
  991. .I b ,
  992. or decimal 11,)
  993. and is declared to be an integer
  994. (as the
  995. .I n_desc
  996. field has value 5, the basic type is
  997. .I integer ,
  998. and there are no qualifications.)
  999. The next entry, number 12, contains a bit more information about ``i'';
  1000. it is four bytes long.
  1001. As a final example, look at entry 28.
  1002. It marks the address (that is, 0x51 in the object file)
  1003. which corresponds to the beginning of a source line
  1004. (namely, line 22 in the source file).
  1005. Since, according to the next entry,
  1006. line 23 also begins at that address,
  1007. line 22 is probably blank.
  1008. Indeed, that is the case.
  1009. .PP
  1010. Now, let's look at the way these symbols interact with the
  1011. text and relocation segments.
  1012. Here is partial output from the
  1013. .SM
  1014. VAX\c
  1015. .LG
  1016. -11/750 disassembler
  1017. .I unas75 (1)
  1018. that corresponds to the text segment;
  1019. it has been edited for the convenience of the reader.
  1020. .DS B
  1021. .ls 1
  1022. .ds Ap \o"' "
  1023. .ta \w#0x00: #u +\w#00 00 00 00' #u +7m +6m +10m
  1024. 0x00:    00 08    _main:    .word    ^M<r11>    ; x.c
  1025. 0x02:    11 5b        brb    0x5f    ; line 11
  1026. 0x04:   de ef 5a\*(Ap00\*(Ap            moval   0x64,oops       ; lines 12 - 13
  1027.         00\*(Ap00\*(Apad fc
  1028. 0x0c:    d1 ac 04 02        cmpl    argc,$2    ; lines 14 - 15
  1029. 0x10:    13 1c        beql    0x2e
  1030. 0x12:    d0 ac 08 50        movl    argv,r0    ; line 16
  1031. 0x16:    dd 60        pushl    (r0)
  1032. 0x18:   dd 8f 6d\*(Ap00\*(Ap            pushl   $109
  1033.         00\*(Ap00\*(Ap
  1034. 0x1e:   fb 02 ef db\*(Ap            calls   $2,_printf
  1035.         ff\*(Apff\*(Apff\*(Ap
  1036. 0x25:    dd 00        pushl    $0    ; line 17
  1037. 0x27:   fb 01 ef d2\*(Ap            calls   $1,_exit
  1038.         ff\*(Apff\*(Apff\*(Ap
  1039. 0x2e:    dd 00        pushl    $0    ; lines 18 - 20
  1040. 0x30:    d0 ac 08 50        movl    argv,r0
  1041. 0x34:    dd a0 04        pushl    4(r0)
  1042. 0x37:   fb 02 ef c2\*(Ap            calls   $2,_access
  1043.         ff\*(Apff\*(Apff\*(Ap
  1044. 0x3e:    d1 50 8f ff        cmpl    r0,$-1
  1045.     ff ff ff
  1046. 0x45:    12 0a        bneq    0x51
  1047. 0x47:    dd ad fc        pushl    oops    ; line 21
  1048. 0x4a:   fb 01 ef af\*(Ap            calls   $1,_perror
  1049.         ff\*(Apff\*(Apff\*(Ap
  1050. 0x51:   dd ef a9\*(Apff\*(Ap            pushl   _errno  ; lines 22 - 23
  1051.         ff\*(Apff\*(Ap
  1052. 0x57:   fb 01 ef a2\*(Ap            calls   $1,_exit
  1053.         ff\*(Apff\*(Apff\*(Ap
  1054. 0x5e:    04        ret        ; line 24
  1055. 0x5f:    c2 04 5e        subl2    $4,sp
  1056. 0x62:    11 a0        brb    0x4
  1057. .ls
  1058. .DE
  1059. The leftmost column contains the address of the instruction,
  1060. the second column the actual machine code,
  1061. and the rest of the line is the equivalent assemble language code.
  1062. The comments, which begin with `;', reflect the contents of the symbol
  1063. table.
  1064. .PP
  1065. This also shows how relocation data is used.
  1066. (The apostrophes after the bytes in the machine code column indicate
  1067. that that byte is relocatable.)
  1068. The relocation bits for the text segment are:
  1069. .DS B
  1070. .ls 1
  1071. .ta \w'0: 'u +\w'00, 00, 0, 0, 0  'u +\w'0: 'u
  1072. 0:    6, 6, 1, 2, 0    4:    58, 25, 1, 2, 1
  1073. 1:    26, 6, 0, 2, 0    5:    77, 27, 1, 2, 1
  1074. 2:    33, 19, 1, 2, 1    6:    83, 30, 1, 2, 1
  1075. 3:    42, 21, 1, 2, 1    7:    90, 21, 1, 2, 1
  1076. .ls 1
  1077. .DE
  1078. Bearing in mind that the libraries used by the startup routine,
  1079. mainly the standard library, occupies 388 bytes of data space,
  1080. let us calculate the value put in for ``_errno'' in the executable file.
  1081. After creating the executable file, we can examine the header block
  1082. (using either
  1083. .I osho (1)
  1084. or
  1085. .I unas (1)),
  1086. and we see the text segment is 5120 bytes long.
  1087. As there are 388 bytes of data loaded at the head of the data segment
  1088. by the startup routine, then, the final address of the variable ``_errno''
  1089. is 5120 + 388 or 5508 (that is, 0x1584).
  1090. Now, the startup routine is 60 bytes long;
  1091. the value in the word to be relocated is \-87;
  1092. hence, the final value put in that word is
  1093. 5508 \- 60 + (\-87) = 5361, or 0x14f1.
  1094. That's all there is to it.
  1095. .SH
  1096. Conclusion
  1097. .PP
  1098. This note is a collection of information
  1099. about object and
  1100. .I a.out
  1101. files.
  1102. Its purpose is to enable those who need this information
  1103. to find it quickly and easily,
  1104. rather than having to dig through the manual and
  1105. the source code for several assorted programs.
  1106. .PP
  1107. For most applications,
  1108. this document contains far more details than are necessary.
  1109. Indeed, as the meaning of the symbols in the symbol table change to allow
  1110. more sophistication among
  1111. .UX
  1112. debuggers,
  1113. some of the information here may become incomplete or obsolete.
  1114. However, knowledge of the relocatable and executable file
  1115. formats is essential for developing new
  1116. .UX
  1117. tools, new versions of
  1118. .UX ,
  1119. and for porting
  1120. .UX
  1121. to new computers;
  1122. this paper is intended to summarize
  1123. the information scattered throughout other
  1124. .UX
  1125. documents.
  1126. .SH
  1127. Acknowledgements
  1128. .PP
  1129. This paper sprung from work done in the summer of 1982
  1130. at Megatest Corporation.
  1131. Mike DeMoney, Dave Emberson, Gary Fine, Steve Stone, and Mike Yip
  1132. all contributed to this document; my thanks to them all.
  1133. Thanks also to all my co-workers at Megatest;
  1134. without such a pleasant and creative environment, this document
  1135. would never have been written.
  1136. .SH
  1137. Sources
  1138. .PP
  1139. The manual page for
  1140. .I a.out (5)
  1141. contains information on the layout of an executable file;
  1142. .I stabs (5)
  1143. describes the meaning of entries in the symbol table
  1144. when the
  1145. .I g
  1146. flag is given.
  1147. The code for the loader program
  1148. .I ld.c
  1149. and for the
  1150. .I unas (1)
  1151. disassemblers (Version 1.2)
  1152. were invaluable.
  1153. .SH
  1154. Appendix \- Format of a
  1155. .UX
  1156. Version 7
  1157. \kx\fIa.out\fP\h'|\nxu+2u'\fIa.out\fP
  1158. File
  1159. .PP
  1160. This section provides a brief summary of the format
  1161. of a
  1162. .UX
  1163. Version 7
  1164. .I a.out
  1165. file;
  1166. it is intended for those who are
  1167. interested in the format of such
  1168. files on the system from which
  1169. 4.1BSD was developed.
  1170. The main difference, of course, is that Version 7 was designed
  1171. for 16 bit machines in general (and the
  1172. \s-2PDP-11\s0\*(n1 in particular)
  1173. .FS
  1174. \*(n1\s-2PDP\s0 is a Trademark of Digital Equipment Corporation.
  1175. .FE
  1176. .PP
  1177. The header for this version is similar to
  1178. that of the 4.1BSD version;
  1179. the only significant difference is that
  1180. only the existence, and not the size, of the
  1181. relocation segment or segments are recorded.
  1182. The structure for the header is
  1183. .DS B
  1184. .ls 1
  1185. .ta \w'#define'u +\w'unsigned  'u +\w'a_dirsize  'u +4n
  1186. struct exec {
  1187.     int    a_magic;    /* magic number */
  1188.     unsigned    a_text;    /* size of text segment */
  1189.     unsigned    a_data;    /* size of initialized data */
  1190.     unsigned    a_bss;    /* size of uninitialized data */
  1191.     unsigned    a_syms;    /* size of symbol table */
  1192.     unsigned    a_entry;    /* entry point */
  1193.     unsigned    a_unused;    /* not used */
  1194.     unsigned    a_flag;    /* relocation info stripped */
  1195. };
  1196. .ls
  1197. .DE
  1198. Note that each of these fields is 2 bytes long;
  1199. so, the header occupies 16 bytes (and is half as large as
  1200. the 4.1BSD header.)
  1201. The sizes are in bytes, and are even.
  1202. .PP
  1203. The starting address of each segment may be computed from this
  1204. structure, as follows:
  1205. .DS B
  1206. .ls  1
  1207. .ta \w'relocation information  'u +\w'beg'u
  1208. \fIpart of file\fP    \fIbegins at location\fP
  1209. text segment    020
  1210. data segment    020 + \fBa_text\fP
  1211. relocation information    020 + \fBa_text\fP + \fBa_data\fP
  1212.         \fIif present\fP
  1213. symbol table    020 + \fBa_text\fP + \fBa_data\fP
  1214.         \fIwithout relocation information\fP
  1215. symbol table    020 + 2 * (\fBa_text\fP + \fBa_data\fP)
  1216.         \fIwith relocation information\fP
  1217. .ls
  1218. .DE
  1219. .PP
  1220. There are four magic numbers;
  1221. each causes the file to be loaded differently.
  1222. .Bs A_MAGIC1 "(value 0407)"
  1223. This is the same as the
  1224. .B OMAGIC
  1225. magic number in 4.1BSD.
  1226. .Es
  1227. .Bs A_MAGIC2 "(value 0410)"
  1228. The data segment in files with this magic number
  1229. begins at the block immediately following the last block
  1230. containing any of the text segment
  1231. (on a \s-2PDP-11\s0, a block is 256 bytes);
  1232. as for
  1233. .B NMAGIC ,
  1234. the text segment is write-protected and shared by
  1235. all processes executing the file.
  1236. .Es
  1237. .Bs A_MAGIC3 "(value 0411)"
  1238. This is just like
  1239. .B A_MAGIC2 ,
  1240. except that the instruction and data spaces are
  1241. separate; both begin at location 0.
  1242. .Es
  1243. .Bs A_MAGIC4 "(value 0405)"
  1244. This is used for overlays;
  1245. the text segment is overlaid on an existing text segment
  1246. (from a file with magic number
  1247. .B A_MAGIC3
  1248. or
  1249. .B A_MAGIC4 )
  1250. while the existing data segment is preserved.
  1251. .Es
  1252. .PP
  1253. As on the
  1254. .SM
  1255. VAX\c
  1256. .LG
  1257. , the stack begins at the highest possible location
  1258. (0177776 on the \s-2PDP\s0)
  1259. and grows down; although it is automatically
  1260. extended as required,
  1261. the data segment is only extended when
  1262. .I sbrk (2)
  1263. is called.
  1264. .PP
  1265. Relocation data amounts to one word per word of text or
  1266. (initialized) data segment.
  1267. The high 12 bits of the word contain the ordinal of the symbol
  1268. in the symbol table (remember, the first symbol is number 0!)
  1269. being relocated;
  1270. the low order bit indicates if the reference is pc-relative;
  1271. and bits 3, 2, and 1 indicate the segment referred to by the relocation word,
  1272. with values
  1273. .DS B
  1274. .ls 1
  1275. .ta \w'000  'u
  1276. 00    absolute
  1277. 01    relative to the text segment
  1278. 02    relative to (initialized) data segment
  1279. 03    relative to bss (uninitialized data) segment
  1280. 04    undefined external symbol
  1281. .ls
  1282. .DE
  1283. .PP
  1284. The symbol table entries are quite different.
  1285. Their structure is
  1286. .DS B
  1287. .ls 1
  1288. .ta \w'#define 'u +\w'unsigned 'u +\w'n_name[8]; 'u
  1289. struct nlist {        /* symbol table entry */
  1290.     char    n_name[8];    /* symbol name */
  1291.     int    n_type;    /* type flag */
  1292.     unsigned    n_value;    /* value */
  1293. }
  1294. .ls
  1295. .DE
  1296. Note that a name may have at most 8 characters;
  1297. no string table is necessary since the name can be stored
  1298. in the entry itself.
  1299. Legal types are
  1300. .DS B
  1301. .ls 1
  1302. .ta \w'N_MMMM  'u +\w'(000)  'u
  1303. \fBN_UNDF\fP    (000)    not associated with any segment
  1304. \fBN_ABS\fP    (001)    absolute address (value) given
  1305. \fBN_TEXT\fP    (002)    located in the text segment
  1306. \fBN_DATA\fP    (003)    located in the initialized data segment
  1307. \fBN_BSS\fP    (004)    located in the uninitialized data segment
  1308. \fBN_REG\fP    (024)    register name
  1309. \fBN_FN\fP    (037)    names a file
  1310. \fBN_EXT\fP    (040)    defined externally
  1311. .ls
  1312. .DE
  1313. Note that the external type may be or'ed in with any of the others.
  1314.